pp=$(basename $(pwd)); 
id=${pp:11:2}

echo $id

if [ $id == 10 -o $id == 20 -o  $id == 30 ]
    then
    pretag=Renormalized_
fi

mcid=$( expr $id + 50 )

rm -f *.eps Frac*.root

pwd > gsi_dir
ls
echo
echo

klogx=0
if [ ${id:0:1} == 1 -o ${id:1:1} == 1 -o ${id:1:1} == 2 ]
then
    klogx=1
fi

for ipar in $(echo 2 0 1 3 )
  do

  kyys=$(echo 0);
  if [ $ipar == 3 ]
      then
      if [ ${id:1:1} == 0 -o ${id:1:1} == 3 ]
      then
          kyys=$(echo 1);
      fi
  fi

  if [ ${id:1:1} == 5 ]
  then
      kyys=$(echo 1);
  fi

  if [ ${id:1:1} == 0 -o ${id:1:1} == 3 ]
  then
      if [ $ipar == 0 -o $ipar == 2 ]
      then
          kyys=$(echo 0 1)
      fi
  fi

  for klogy in $kyys
  do 

#IDFF RecMC Jpt
  for mm in $(echo ../TPC*); do

      echo $mm
      tt=$(basename $mm)
      pp=${tt:0:4}
      echo $pp
          

      #for mm in $(echo ../TPC1TOF4); do
      #for jpt in $(echo  5-10 10-15 15-20 20-100  -5-100)
      #for jpt in $(echo  5-10 10-15 15-20)
      #for jpt in $(echo 5-6 6-7 7-8 8-9 9-10 5-7 10-15 10-16 10-17 15-20 10-20)
      #for jpt in $(echo 5-6 6-7 7-8 8-9 9-10 5-10 10-15 15-20)
      #for jpt in $(echo 5-10 10-15 15-20)
      for jpt in $(echo 5-6 5-7 10-15 10-16 10-17 10-20)
      do
          if [ 1 == 2 -a ${id:0:1} != 0 ]; then
              tag=RecMCDF$tt$jpt

              #f1=$(find $(echo $pretag\cmpEta_$tt\_GrID$mcid\*JPT$jpt\_*.root))

              f1=$(find $(echo $pretag*$pp\TOF0*JPT$jpt\_directMC_id$mcid.root))
              f2=$(find $(echo $pretag\cmpEta_$tt\_GrID$id\*JPT$jpt\_*.root))
              f3=
              f4=
              f5=
              f6=
                #root -b -l -q 'drawFractionRatio.C("'$tag'_",'$ipar', "'$f1'","'$f2'","'$f3'","'$f4'","'$f5'","'$f6'",0,"scaled to ref.",59,'$klogx','$klogy')'
              #./drawFractionRatio "$tag"_ $ipar "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 59 $klogx $klogy
              ./drawFractionRatio "$tag"_ $ipar "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy
              
          fi

          if [ 1 == 5 -a ${id:0:1} != 0 ]; then
              tag=RecMCDP$tt$jpt

              if [ ${jpt:0:2} == 5- ]
              then
                  bpt=5-6
              elif [ ${jpt:0:2} == 10 ]
              then
                  bpt=10-11
              elif [ ${jpt:0:2} == 15 ]
              then
                  bpt=15-16
              fi

              f1=$(find $(echo $pretag*$pp\TOF0*JPT$jpt\_directMC_id$mcid.root))
              #f2=$(find $(echo $pretag*$pp\TOF0*JPT$bpt\_directMC_id$mcid.root))
              f2=$(find $(echo $pretag\cmpEta_$tt\_GrID$mcid\*JPT$jpt\_*.root))
              #f4=$(find $(echo $pretag\cmpEta_$tt\_GrID$mcid\*JPT$bpt\_*.root))
              f3=
              f4=
              f5=
              f6=
              ./drawFractionRatio "$tag"_ $ipar "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy
              
          fi
          
          if [ 1 == 2 -a $id == 10 ]; then
              tag=cmpMCBeta$tt$jpt
              f1=$(find $(echo $pretag\cmpEta_$tt\_GrID$id\*JPT$jpt\_*.root))
              f2=$(find $(echo Beta*$pretag\cmpEta_$tt\_GrID$id\*JPT$jpt\_*.root))
              f3=
              f4=
              f5=
              f6=
              ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 59 $klogx $klogy

          fi

          if [ 1 == 0 ]; then
              tag=rb1$tt$jpt
              f1=$(find $(echo $pretag\cmpEta_$tt\_GrID$id\*JPT$jpt\_*.root))
              f2=$(find $(echo rb1$pretag\cmpEta_$tt\_GrID$id\*JPT$jpt\_*ETAALL.root))
              f3=
              f4=
              f5=
              f6=
              ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 59 $klogx $klogy

          fi
            
      done
  done

  if [ 1 == 1 ]; then
      tag=cmpJpt$tt
      f1=$(find $pretag*$tt*GrID$id*JPT15-20\_*.root)
      f2=$(find $pretag*$tt*GrID$id*JPT10-15\_*.root)
      f3=$(find $pretag*$tt*GrID$id*JPT5-10\_*.root)
      f4=
      f5=
      f6=
      
      ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 55 $klogx $klogy
      fi

########
#only for pt logx1
  #for jpt in $(echo  5-10 10-15 15-20 20-100  -5-100)
  for jpt in $(echo  5-10 10-15 15-20 )
  do                
      
      if [ 1 == 0 ]; then            
#comp TPC1TOF0 vs TPC1TOF$qq
          tt=TOF
          
          tag=cmpTOF1$tt\-$jpt
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF1_GrID$id\*JPT$jpt\_*.root))
#only consider effect on fit, no need for direct truth
#$(find $(echo $pretag*TPC1TOF0*JPT$jpt\_directMC_id$mcid.root))   
          f4=
          f5=
          f6=
            #root -b -l -q 'drawFractionRatio.C("'$tag'",'$ipar', "'$f1'","'$f2'","'$f3'","'$f4'","'$f5'","'$f6'",0,"scaled to ref.",27,'$klogx','$klogy')'
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 

          tag=cmpTOF2$tt\-$jpt
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF2_GrID$id\*JPT$jpt\_*.root))
          f4=
          f5=
          f6=
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 

          tag=cmpTOF3$tt\-$jpt
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF3_GrID$id\*JPT$jpt\_*.root))
          f4=
          f5=
          f6=
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 

          tag=cmpTOF4$tt\-$jpt
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF4_GrID$id\*JPT$jpt\_*.root))
          f4=
          f5=
          f6=
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 

          tag=cmpTOFall$tt\-$jpt
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF2_GrID$id\*JPT$jpt\_*.root))
          f4=$(find $(echo $pretag\cmpEta_TPC1TOF3_GrID$id\*JPT$jpt\_*.root))
          f5=$(find $(echo $pretag\cmpEta_TPC1TOF4_GrID$id\*JPT$jpt\_*.root))
          f6=
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 
          
          if [ 1 == 0 ]; then
              tag=cmp$tt\B-$jpt
              f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$mcid\*JPT$jpt\_*.root))
              f2=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
              f3=$(find $(echo $pretag\cmpEta_TPC1TOF4_GrID$id\*JPT$jpt\_*.root))
              f4=
              f5=
              f6=
              ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 
              
              tag=cmp$tt\-$jpt
              f1=$(find $(echo $pretag\cmpEta_TPC1TOF9_GrID$id\*JPT$jpt\_*.root))
              f2=$(find $(echo $pretag\cmpEta_TPC1TOF4_GrID$id\*JPT$jpt\_*.root))
#only consider effect on fit, no need for direct truth
#$(find $(echo $pretag*TPC1TOF0*JPT$jpt\_directMC_id$mcid.root))   
              f3=
              f4=
              f5=
              f6=
            #root -b -l -q 'drawFractionRatio.C("'$tag'",'$ipar', "'$f1'","'$f2'","'$f3'","'$f4'","'$f5'","'$f6'",0,"scaled to ref.",27,'$klogx','$klogy')'
              ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy 
          fi
      fi
      
      if [ 1 == 0 ]; then
#only for MC fit
#comp TPC1TOF0 vs TPC1TOF$qq
          tt=trueTOF
          tag=cmp$tt\-$jpt
          f1=$(find $(echo $pretag*TPC1TOF0*JPT$jpt\_directMC_id$mcid.root))   
#$(find $(echo $pretag\cmpEta_TPC1TOF0_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC1TOF1_GrID$mcid\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF2_GrID$mcid\*JPT$jpt\_*.root))
          f4=$(find $(echo $pretag\cmpEta_TPC1TOF3_GrID$mcid\*JPT$jpt\_*.root))
          f5=$(find $(echo $pretag\cmpEta_TPC1TOF4_GrID$mcid\*JPT$jpt\_*.root))
          f6=
            #root -b -l -q 'drawFractionRatio.C("'$tag'",'$ipar', "'$f1'","'$f2'","'$f3'","'$f4'","'$f5'","'$f6'",0,"scaled to ref.",27,'$klogx','$klogy')'
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 27 $klogx $klogy
      fi
      
      if [ 1 == 2 ]; then            
#comp TPC1TOF3 vs TPC2TOF3
          tag=cmpTPCA$jpt
#$(find $(echo $pretag*TPC1TOF0*JPT$jpt\_directMC_id$mcid.root))
#$(find $(echo $pretag*TPC2TOF0*JPT$jpt\_directMC_id$mcid.root))
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9\_GrID$mcid\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC2TOF9\_GrID$mcid\*JPT$jpt\_*.root))
          f3=$(find $(echo $pretag\cmpEta_TPC1TOF9\_GrID$id\*JPT$jpt\_*.root))
          f4=$(find $(echo $pretag\cmpEta_TPC2TOF9\_GrID$id\*JPT$jpt\_*.root))
          f5=
          f6=
            #root -b -l -q 'drawFractionRatio.C("'$tag'",'$ipar', "'$f1'","'$f2'","'$f3'","'$f4'","'$f5'","'$f6'",0,"scaled to ref.",43,'$klogx','$klogy')'
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 43 $klogx $klogy
          
          if [ 1 == 2 ]; then
          tag=cmpTPC$jpt
          f1=$(find $(echo $pretag\cmpEta_TPC1TOF9\_GrID$id\*JPT$jpt\_*.root))
          f2=$(find $(echo $pretag\cmpEta_TPC2TOF9\_GrID$id\*JPT$jpt\_*.root))
          f3=
          f4=
          f5=
          f6=
            #root -b -l -q 'drawFractionRatio.C("'$tag'",'$ipar', "'$f1'","'$f2'","'$f3'","'$f4'","'$f5'","'$f6'",0,"scaled to ref.",43,'$klogx','$klogy')'
          ./drawFractionRatio "$tag" $ipar  "$f1" "$f2" "$f3" "$f4" "$f5" "$f6" 0 "scaled to ref." 43 $klogx $klogy
          fi

      fi            
      
  done
done
      
###     
done


exit


rm -rf inverse
mkdir -p inverse

mv Invers*.eps inverse
